<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">

          <a-col :md="8" :sm="24">
            <a-form-item label="发货单号">
              <a-input v-model="queryParam.orderNumber" placeholder="请输入发货单号"/>
            </a-form-item>
          </a-col>

          <a-col :md="!advanced && 8 || 24" :sm="24">
            <span class="table-page-search-submitButtons" :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <!--<a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>-->
            </span>
          </a-col>

          <a-col :md="24" :sm="24" v-if="canReadOrgIdCompany()">
            <a-form-item label="供应商：">
              <a-radio-group default-value="" button-style="solid" v-model="queryParam.orgIdSupplier">
                <a-radio-button value="">
                  全部
                </a-radio-button>
                <a-radio-button :value="id" v-for="{id, name} in $util.getOrgList([this.$businessEnum.ORG_TYPE_ENUM.PRIMARY_SUPPLIER.id, this.$businessEnum.ORG_TYPE_ENUM.NORMAL_SUPPLIER.id])">
                  {{ name }}
                </a-radio-button>
              </a-radio-group>
            </a-form-item>
          </a-col>

        </a-row>
      </a-form>
    </div>

    <s-table
      ref="table"
      size="default"
      :columns="canReadOrgIdCompany()?columns:columns1"
      :data="loadData"
      :alert="true"
      :rowKey="(record) => record.orderId"
      :rowSelection="null"
    >
      <span slot="action" slot-scope="text, record">
       <a @click="$refs.inStoreDetailForm.show(record)">入库单明细</a>
      </span>
    </s-table>

    <in-store-detail-form ref="inStoreDetailForm" @ok="handleOk"/>

  </a-card>
</template>

<script>
import {STable} from '@/components'
import {page} from '@/api/modular/main/emsorder/emsOrderManage'

import InStoreDetailForm from './inStoreDetailForm'

export default {
  components: {
    STable,
    InStoreDetailForm,
  },
  data() {
    return {
      advanced: false, // 高级搜索 展开/关闭
      queryParam: {},  // 查询参数
      columns: [
        {
          title: '序号',
          width: '80px',
          customRender: (text, record, index) => `${index + 1}`
        },
        {
          title: '发货单号',
          dataIndex: 'orderNumber'
        },
        {
          title: '供应商',
          dataIndex: 'supplierName',
        },
        {
          title: '发货时间',
          dataIndex: 'createTime'
        },
        {
          title: '操作',
          dataIndex: '',
          width: '250px',
          scopedSlots: {customRender: 'action'}
        }
      ],
      columns1: [
        {
          title: '序号',
          width: '80px',
          customRender: (text, record, index) => `${index + 1}`
        },
        {
          title: '发货单号',
          dataIndex: 'orderNumber'
        },
        // {
        //   title: '供应商',
        //   dataIndex: 'targetOrgName',
        // },
        {
          title: '发货时间',
          dataIndex: 'createTime'
        },
        {
          title: '操作',
          dataIndex: '',
          width: '250px',
          scopedSlots: {customRender: 'action'}
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.queryParam.orderType = this.$businessEnum.ORDER_TYPE_ENUM.deliverOrder.id
        this.queryParam.executeStatus = this.$businessEnum.EXECUTE_STATUS_ENUM.COMPLETED.id
        this.queryParam.materialType = this.materialType || this.$businessEnum.MATERIAL_TYPE_ENUM.material.id
        this.queryParam.isOverseasStore = 'N'
        return page(Object.assign(parameter, this.queryParam)).then((res) => {
          return res.data
        })
      },
      scheduleProductionVisible: false,
      selectedRowKeys: [],
      selectedRows: [],
      defaultExpandedKeys: [],
      replaceFields: {
        key: 'id',
      },
    }
  },
  computed: {
    routePath() {
      let urlParam = this.$util.getUrlParam(this.$route.path)
      return urlParam
    },
    materialType() {
      return this.routePath.materialType
    },
  },
  watch: {
    '$route.path': function (newValue) {
      setTimeout(() => {
        this.$refs.table.refresh(true)
        this.$refs.table.clearSelected()
      }, 100)
    }
  },
  methods: {
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    handleOk() {
      this.$refs.table.refresh()
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    canReadOrgIdCompany() {
      return !this.hasRole(this.$util.ROLE_ENUM.GS_ADMIN.id) 
      && !this.hasRole(this.$util.ROLE_ENUM.MATERIAL_ADMIN3.id)
       && !this.hasRole(this.$util.ROLE_ENUM.MATERIAL_ADMIN2.id)
       && !this.hasRole(this.$util.ROLE_ENUM.SQCG.id)

    },
  }
}
</script>